package com.sise.cn.controller;

import com.sise.cn.annotation.AccessAuthority;
import com.sise.cn.api.common.service.OrderItemService;
import com.sise.cn.task.po.JobPO;
import com.sise.cn.task.service.ScheduledTask;
import com.sise.cn.task.service.impl.DelayTaskImpl;
import com.sise.cn.task.service.impl.MessageSend;
import com.sise.cn.task.service.impl.OneTask;
import com.sise.cn.task.service.impl.ScheduleTaskImpl;
import com.sise.cn.task.vo.ServerResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 龚秀峰
 * @version 1.0
 * @date 2020/6/4 17:45
 * 消息接口
 */
@RestController
@RequestMapping("/task")
@Slf4j
public class TaskController {

    @Resource
    private MessageSend messageSend;


    @Autowired(required = false)
    private Map<String, OrderItemService> orderItemServiceMap;

    @Value("${product.wechat.index-url}")
    private String url;

    @GetMapping("/task")
    @AccessAuthority
    public ServerResponseVO<String> productTask() {
        JobPO j1 = new JobPO();
        j1.setName("name");
        j1.setJobGroup("group");
        j1.setTriggerGroup("triggerGroup");
        j1.setTriggerName("triggerName");
        j1.setJobCorn("0/20 * * * * ?");
        ScheduledTask.addScheduleJob(j1, DelayTaskImpl.class);
        JobPO j2 = new JobPO();
        j2.setName("name1");
        j2.setJobGroup("group1");
        j2.setTriggerGroup("triggerGroup1");
        j2.setTriggerName("triggerName1");
        j2.setJobCorn("0/5 * * * * ?");
        ScheduledTask.addScheduleJob(j2, ScheduleTaskImpl.class);

        ScheduledTask.submitTask(OneTask.class);
        return ServerResponseVO.success();
    }

    @GetMapping("/sendDelay")
    @AccessAuthority
    public ServerResponseVO<String> sendDelayMessage(@RequestParam(value = "id") String id) {
        messageSend.sendDelayTask(id, 3000);
        return ServerResponseVO.success();
    }

    @GetMapping("/send")
    @AccessAuthority
    public ServerResponseVO<String> sendMessage(@RequestParam(value = "id") String id) {
        messageSend.sendTask(id);
        return ServerResponseVO.success();
    }

    @PostMapping("/value")
    @AccessAuthority
    public Object value() {
        Map<String, Object> map = new HashMap<>(2);
        map.put("key", url);
        return map;
    }

}
